﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO;

namespace DA
{
    public class SanPhamDB
    {
        public List<SanPhamData> getAllSanPham()
        {
            List<SanPhamData> list = new List<SanPhamData>();
            string sql = "select * from sanpham";
            ConnectionDB cn = new ConnectionDB();
            cn.Cmd.CommandText = sql;
            SqlDataReader reader = cn.Cmd.ExecuteReader();
            SanPhamData data=null;
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    data = new SanPhamData();
                    data.MaSP = reader.GetString(0);
                    data.TenSP = reader.GetString(1);
                    data.Gia = reader.GetDecimal(2);
                    data.HinhAnh = reader.GetString(6);
                    list.Add(data);
                }
            }
            return list;
        }
        public bool ThemSanPham(SanPhamData data)
        {
            try
            {
                ConnectionDB cn = new ConnectionDB();
                string sql = "Insert into SanPham(MaSP,TenSP,Gia,ThongTin,MaLoai,SL,HinhAnh,MaNSX,GiaNhap,TinhTrang) values(@MaSP,@TenSP,@Gia,@ThongTin,@MaLoai,@SL,@HinhAnh,@MaNSX,@GiaNhap,@TinhTrang)";
                cn.Cmd.Parameters.Add(new SqlParameter("@MaSP",data.MaSP));
                cn.Cmd.Parameters.Add(new SqlParameter("@TenSP",data.TenSP));
                cn.Cmd.Parameters.Add(new SqlParameter("@Gia",data.Gia));
                cn.Cmd.Parameters.Add(new SqlParameter("@ThongTin",data.ThongTin));
                cn.Cmd.Parameters.Add(new SqlParameter("@MaLoai",data.MaLoai));
                cn.Cmd.Parameters.Add(new SqlParameter("@SL",data.Sl));
                cn.Cmd.Parameters.Add(new SqlParameter("@HinhAnh", data.HinhAnh));
                cn.Cmd.Parameters.Add(new SqlParameter("@MaNSX", data.MaNXS));
                cn.Cmd.Parameters.Add(new SqlParameter("@GiaNhap",data.GiaNhap));
                cn.Cmd.Parameters.Add(new SqlParameter("@TinhTrang", data.TinhTrang));
                cn.Cmd.CommandText = sql;
                cn.Cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
    }
}
